niu
<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <div class="data-table__toolbar--actions">
          <el-button type="primary" plain icon="plus" @click="handleOpenDialog()">新增</el-button>
        </div>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableDatas"
        highlight-current-row
        border
        @select="handleSelectionChange"
        class="data-table__content"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="仓库名称" prop="name" width="150" />
        <el-table-column label="编码" prop="code" width="150" />
        <el-table-column label="系统内置" width="150">
          <template #default="scope">
            <el-tag v-if="scope.row.systemd" type="success">内置</el-tag>
            <el-tag v-else type="info">自定义</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" min-width="150" prop="address" />
        <el-table-column label="描述" align="center" min-width="150" prop="remark" />
        <el-table-column fixed="right" label="操作" width="300">
          <template #default="scope">
            <el-button type="primary" link icon="edit" @click="gotoInOperation(scope.row.id)">
              入库
            </el-button>
            <el-button type="primary" link icon="edit" @click="gotoOutOperation(scope.row.id)">
              出库
            </el-button>
            <el-button
              type="primary"
              :disabled="scope.row.systemd"
              size="small"
              link
              icon="edit"
              @click="handleOpenDialog(scope.row.id)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              :disabled="scope.row.systemd"
              size="small"
              link
              icon="delete"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="fetchData"
      />
    </el-card>

    <!-- 客户表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="roleFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入仓库名称" />
        </el-form-item>

        <el-form-item label="仓库编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入仓库编码" />
        </el-form-item>

        <el-form-item label="仓库地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入仓库地址" />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="formData.remark" type="textarea" placeholder="请输入仓库描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import router from "@/router";
import type { ElTable } from "element-plus";
import WarehouseAPI, {
  WarehousePageQuery,
  WarehouseForm,
  WarehousePageVO,
} from "@/api/business/warehouse/warehouse.api";

defineOptions({
  name: "Warehouse",
  inheritAttrs: false,
});

const dataTableRef = ref<InstanceType<typeof ElTable>>();

const queryFormRef = ref();
const roleFormRef = ref();

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<WarehousePageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 客户表格数据
const tableDatas = ref<WarehousePageVO[]>();

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 客户表单
const formData = reactive<WarehouseForm>({});

const rules = reactive({
  name: [{ required: true, message: "请输入仓库名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入仓库编码", trigger: "blur" }],
  address: [{ required: true, message: "请输入仓库地址", trigger: "blur" }],
});

// 获取数据
function fetchData() {
  loading.value = true;
  WarehouseAPI.getPage(queryParams)
    .then((data) => {
      tableDatas.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 查询（重置页码后获取数据）
function handleQuery() {
  queryParams.pageNum = 1;
  fetchData();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  fetchData();
}

// 打开仓库信息弹窗
async function handleOpenDialog(roleId?: string) {
  if (roleId) {
    dialog.title = "修改仓库信息";
    await WarehouseAPI.getFormData(roleId)
      .then((data) => {
        Object.assign(formData, data);
      })
      .finally(() => {
        dialog.visible = true;
      });
  } else {
    dialog.visible = true;
    dialog.title = "新增仓库信息";
  }
}

// 跳转到仓库
function gotoInOperation(id: string) {
  router.push({
    path: "/warehouse/inOperation",
    query: { warehouseId: id, title: "材料入库" },
  });
}

function gotoOutOperation(id: string) {
  router.push({
    path: "/warehouse/outOperation",
    query: { warehouseId: id, title: "材料出库" },
  });
}

const handleSelectionChange = (selection: any[], row: any) => {
  dataTableRef.value?.clearSelection();
  dataTableRef.value?.toggleRowSelection(row, true);
};

// 提交仓库信息表单
function handleSubmit() {
  roleFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (formData.id) {
        WarehouseAPI.update(formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        WarehouseAPI.create(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;

  roleFormRef.value.resetFields();
  roleFormRef.value.clearValidate();
  formData.name = "";
  formData.code = "";
  formData.address = "";
  formData.id = undefined;
  formData.remark = "";
}

// 删除客户
function handleDelete(roleId: string) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    WarehouseAPI.deleteById(roleId)
      .then(() => {
        ElMessage.success("删除成功");
        handleResetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>
